package com.jzr.bedside.controller.api;

import com.jzr.bedside.db1.dto.FoodOrderDto;
import com.jzr.bedside.db1.service.TFoodOrderService;
import com.jzr.bedside.db1.vo.FoodOrderDetailVo;
import com.jzr.bedside.db1.vo.FoodOrderVo;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.common.util.UrlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "食堂菜单接口")
@RestController
@RequestMapping("/api/foodOrder")
public class FoodOrderController {
    @Value("${file.foodPic-download-url}")
    private String downloadUrl;

    @Autowired
    private TFoodOrderService foodOrderService;

    @PostMapping("/get")
    @ApiOperation("获取食堂订单列表")
    public Result<List<FoodOrderVo>> get(@RequestBody FoodOrderDto record){
        List<FoodOrderVo> list = foodOrderService.list(record);
        if(list != null && !list.isEmpty()){
            for(FoodOrderVo orderVo : list){
                List<FoodOrderDetailVo> detailVos = orderVo.getDetails();
                if(detailVos != null && !detailVos.isEmpty()){
                    for(FoodOrderDetailVo d : detailVos){
                        d.setImgUrl(UrlUtil.concat(downloadUrl, d.getImgUrl()));
                    }
                }
            }
        }
        return Result.success("成功",list);
    }

    @PostMapping("/add")
    @ApiModelProperty("订单添加")
    public Result<Boolean> add(@RequestBody FoodOrderVo record){
        if(record == null){
            return Result.fail("参数不能为空");
        }
        if(U.isBlank(record.getPatientCode())){
            return Result.fail("patientCode不能为空");
        }
        record.setOrderDate(LocalDateTime.now());
        if(U.isBlank(record.getTotalQty())){
            return Result.fail("totalQty不能为空");
        }else if(record.getTotalQty().intValue() <= 0){
            return Result.fail("totalQty必须大于0");
        }
        if(U.isBlank(record.getTotalAmount())){
            return Result.fail("totalAmount不能为空");
        }else if(record.getTotalAmount().doubleValue() <= 0){
            return Result.fail("totalAmount必须大于0");
        }
        if(U.isBlank(record.getDetails())){
            return Result.fail("details不能为空");
        }else{
            for(FoodOrderDetailVo detailVo : record.getDetails()){
                if(U.isBlank(detailVo.getMenuId())) {
                    return Result.fail("details->menuId不能为空");
                }
                if(U.isBlank(detailVo.getQty())){
                    return Result.fail("details->qty不能为空");
                }else if(detailVo.getQty().intValue() <=0){
                    return Result.fail("details->qty必须大于0");
                }
                if(U.isBlank(detailVo.getAmount())){
                    return Result.fail("details->amount不能为空");
                }else if(detailVo.getQty().intValue() <=0){
                    return Result.fail("details->amount必须大于0");
                }
            }
        }
        try {
            Integer count = foodOrderService.add(record);
            if(count == null || count.intValue() <=0){
                return Result.fail("订单添加失败");
            }
        }catch (Exception e){
            return Result.fail("订单添加失败,"+e.getMessage());
        }

        return Result.success("订单添加成功",true);
    }

    @PostMapping("/cancel")
    @ApiOperation("订单取消")
    public Result<Boolean> cancel(@ApiParam("订单id") Integer id){
        if(U.isBlank(id)){
            return Result.fail("id不能为空");
        }
        Integer status = foodOrderService.getStatus(id);
        if(U.isBlank(status)){
            return Result.fail("取消失败,订单不存在");
        }
        if(status.intValue() == 2){
            return Result.fail("取消失败,订单已经完成交易");
        }
        Integer count = foodOrderService.cancel(id);
        if(count == null || count.intValue() <=0){
            return Result.fail("订单取消失败");
        }
        return Result.success("订单取消成功",true);
    }

//    @InitBinder
//    public void initBinder(WebDataBinder binder) {
//        //转换日期
//        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
//    }
}
